<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="auto" size="small">
      <el-form-item label="广告名称：" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="广告位置：">
        <el-select v-model="form.type">
          <el-option v-for="item in ADVEERT_TYPE_MAP" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间范围：" prop="startTime">
        <el-date-picker type="datetimerange" placeholder="选择日期" v-model="dateRange"></el-date-picker>
      </el-form-item>
      <el-form-item label="上线/下线：">
        <el-radio-group v-model="form.status">
          <el-radio :value="0">下线</el-radio>
          <el-radio :value="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告图片：">
        <ImageUpload accept="svg,png,jpg,jpeg" v-model="form.pic" />
        <!-- <single-upload v-model="form.pic"></single-upload> -->
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
      <el-form-item label="广告链接：" prop="url">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="广告备注：">
        <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="form.note"> </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button v-if="!route.query.id" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script setup lang="ts">
import { createHomeAdvertise, getHomeAdvertise, updateHomeAdvertise } from '@/api/homeAdvertise'
import { ElMessage, type FormInstance } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { onMounted, ref, computed } from 'vue'
import type { Advertise } from '@/models/advertise'
import { ADVEERT_TYPE_MAP } from '@/constant'

defineOptions({ name: 'HomeAdvertiseDetail' })
const rules = {
  name: [
    { required: true, message: '请输入广告名称', trigger: 'blur' },
    { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' },
  ],
  url: [{ required: true, message: '请输入广告链接', trigger: 'blur' }],
  startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
  endTime: [{ required: true, message: '请选择到期时间', trigger: 'blur' }],
  pic: [{ required: true, message: '请选择广告图片', trigger: 'blur' }],
}
const route = useRoute()
const router = useRouter()
const formRef = ref<FormInstance>()
const form = ref<Advertise>({})

onMounted(async () => {
  if (route.query.id) {
    const response = await getHomeAdvertise(Number(route.query.id))
    form.value = response.data.data
  } else {
    form.value = {}
  }
})

const dateRange = computed({
  get() {
    return [form.value.startTime, form.value.endTime]
  },
  set(val) {
    form.value.startTime = val[0]
    form.value.endTime = val[1]
  },
})

async function onSubmit() {
  formRef.value?.validate()
  if (route.query.id) {
    updateHomeAdvertise(Number(route.query.id), form.value)
    ElMessage.success('修改成功')
  } else {
    createHomeAdvertise(form.value)
    ElMessage.success('提交成功')
  }
  router.back()
  formRef.value?.resetFields()
}

function resetForm() {
  formRef.value?.resetFields()
  form.value = {}
}
</script>
<style scoped>
.input-width {
  width: 70%;
}
</style>
